package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.dal.model.ClearDiffDO;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.dal.mapper.ClearAccountMapper;
import com.baofu.paycore.dal.mapper.ClearBankMapper;
import com.baofu.paycore.dal.mapper.ClearMapper;
import com.baofu.paycore.dal.model.ClearAccountDO;
import com.baofu.paycore.dal.model.ClearBankDO;
import com.baofu.paycore.dal.model.ClearDO;
import com.baofu.paycore.manager.converter.DO2BOConverter;
import com.baofu.paycore.manager.model.RechargeBO;
import com.baofu.paycore.manager.storage.Storage;
import com.baofu.paycore.service.facade.model.PaycorePageDTO;
import com.system.commons.exception.BizServiceException;
import com.system.commons.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 *  充值提现单存储
 * <p/>
 *      1、新增订单
 *      2、更新订单
 *      3、废弃订单
 * </p>
 * User: xin deng  Date: 16/4/27 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Repository
public class ClearManager implements Storage<ClearDO> {

    /**
     * 充值/提现单Mapper
     */
    @Autowired
    private ClearMapper clearMapper;

    /**
     * 充值/提现子单Mapper
     */
    @Autowired
    private ClearBankMapper clearBankMapper;

    /**
     * 充值/提现账户资金单Mapper
     */
    @Autowired
    private ClearAccountMapper clearAccountMapper;


    /**
     * 新增订单
     *
     * @param clearDO 订单信息
     */
    @Override
    public int addOrder(ClearDO clearDO) {

        return clearMapper.insert(clearDO);
    }

    /**
     * 更新订单
     *
     * @param clearDO 订单信息
     */
    @Override
    public int modifyOrder(ClearDO clearDO) {

        return clearMapper.update(clearDO);
    }

    /**
     * 废弃订单
     *
     * @param clearDO 订单信息
     */
    @Override
    public int deleteOrder(ClearDO clearDO) {
        return 0;
    }

    /**
     * 1.根据支付单号查询交易
     *
     * @param payNo 支付单号
     * @return 交易
     */
    public ClearDO queryByPayNo(String payNo){

        ClearDO clearDO = clearMapper.queryClearByPayNo(payNo);
        if(clearDO == null){
            log.error("查询结果集为空:{}",payNo);
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }
        return clearDO;
    }

    /**
     * 2.根据订单号、支付流水号查询交易
     *
     * @return 交易
     */
    public ClearDO queryByOrderNo(String requestOrderNo,String requestBizNo,Date requestDate,String requestSystem){
        ClearDO clearResDO = clearMapper.queryByOrderNo(requestOrderNo, requestBizNo, requestDate,requestSystem);
        if(clearResDO == null){
            log.error("查询结果集为空:{} {} {}",requestOrderNo,requestBizNo,requestDate);
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }
        return clearResDO;
    }

    /**
     * 5.根据支付单号查询完整的充值或提现订单，包含主单和资金单明细
     *
     * @param payNo 支付单号
     * @return 交易
     */
    public RechargeBO queryClearOrderByPayNo(String payNo){

        //充值主单
        ClearDO clearDO = clearMapper.queryClearByPayNo(payNo);
        if (clearDO == null) {
            log.error("查询充值主单信息为空 payNo:{}", payNo);
            throw new PayCoreBizException(PayCoreErrorCode.ORDER_IS_NOT_EXISTED);
        }

        //账务充值资金单
        List<ClearAccountDO> clearAccountDOs = clearAccountMapper.queryClearAccountByPayNo(payNo);
        if (CollectionUtils.isEmpty(clearAccountDOs)) {
            log.error("查询充值账务资金单信息为空 payNo:{}", payNo);
            throw new PayCoreBizException(PayCoreErrorCode.SUB_ORDER_IS_NOT_EXISTED);
        }

        //充值银行单
        ClearBankDO clearBankDO = clearBankMapper.queryClearBankInfo(payNo);
        if (null == clearBankDO) {
            log.error("查询充值银行单信息为空 payNo:{}", payNo);
            throw new PayCoreBizException(PayCoreErrorCode.SUB_ORDER_IS_NOT_EXISTED);
        }

        return DO2BOConverter.buildRechargeBO(clearDO, clearAccountDOs, clearBankDO);
    }

    /**
     * 查询交易信息
     *
     * @param clearDO       请求参数
     * @return              总数
     */
    public int queryClearCount(ClearDO clearDO, PaycorePageDTO paycorePageDTO){

        return clearMapper.queryClearCount(clearDO,paycorePageDTO.getStartDate(),paycorePageDTO.getEndDate());
    }

    /**
     * 分页查询交易信息
     *
     * @param clearDO       请求参数
     * @return              交易信息
     */
    public List<ClearDO> queryClearPage(ClearDO clearDO, PaycorePageDTO paycorePageDTO){

        return clearMapper.queryClearByPage(clearDO, paycorePageDTO.getStartDate(),paycorePageDTO.getEndDate(),paycorePageDTO.getStartRow(), paycorePageDTO.getPageCount());
    }

    /**
     * 状态不一致订单数
     *
     * @param clearDiffDO   请求参数
     * @return              总数
     */
    public int queryPayDiffOrderCount(ClearDiffDO clearDiffDO){
        log.info("状态不一致订单信息总数, 请求开始时间:{}, 结束时间:{}", DateUtil.format(clearDiffDO.getStartDate()),
                DateUtil.format(clearDiffDO.getEndDate()));
        return clearMapper.selectDiffStatusCount(clearDiffDO);
    }

    /**
     * 状态不一致订单信息
     *
     * @param clearDiffDO   请求参数
     * @param beginPage     开始页
     * @param endPage       结束页
     * @return              订单信息
     */
    public List<ClearDiffDO> queryPayDiffOrderPage(ClearDiffDO clearDiffDO, int beginPage, int endPage){
        log.info("状态不一致订单信息, 请求开始时间:{}, 结束时间:{}", DateUtil.format(clearDiffDO.getStartDate()),
                DateUtil.format(clearDiffDO.getEndDate()));

        return clearMapper.selectDiffStatusPage(clearDiffDO, beginPage, endPage);
    }
}
